#include "altgraphicszoomwidget.h"

#include <QGraphicsView>
#include <QGridLayout>
#include <QLabel>
#include <QSlider>
#include <QSpinBox>

namespace Alt {
     const int GraphicsZoomWidget::DefaultMaxZoomLevel = 10000;

     GraphicsZoomWidget::GraphicsZoomWidget(QGraphicsView* view, QWidget* parent) : QWidget(parent) {
          init();
          setView(view);
     }

     int GraphicsZoomWidget::maxZoomLevel() const {
          return m_maxZoom;
     }

     void GraphicsZoomWidget::setMaxZoomLevel(int maxZoom) {
          if (maxZoom >= 100) {
               m_maxZoom = maxZoom;
               m_zoomSlider->setMaximum(m_maxZoom);
               m_zoomSpin->setMaximum(m_maxZoom);
          }
          return;
     }

     void GraphicsZoomWidget::setView(QGraphicsView* view) {
          m_view = view;
          setZoomLevel(m_zoomLevel);
          return;
     }

     QGraphicsView* GraphicsZoomWidget::view() const {
          return m_view;
     }

     int GraphicsZoomWidget::zoomLevel() const {
          return m_zoomLevel;
     }

     //public slots
     void GraphicsZoomWidget::setZoomLevel(int zoomLevel) {
          if ( (zoomLevel != m_zoomLevel) && (zoomLevel >= 0) ) {
               m_zoomLevel = zoomLevel;
               emit zoomLevelChanged(m_zoomLevel);
          }
          if (m_view != 0) {
               if (m_zoomLevel == 0) {
                    m_view->fitInView(m_view->sceneRect(), Qt::KeepAspectRatio);
               } else {
                    QMatrix matrix;
                    qreal scale = (qreal)m_zoomLevel / 100.0;
                    matrix.scale(scale, scale);
                    m_view->setMatrix(matrix, false);
               }
          }
          return;
     }

     void GraphicsZoomWidget::zoomToFit() {
          setZoomLevel(0);
          return;
     }

     //private
     void GraphicsZoomWidget::init() {
          m_zoomLevel = 0;

          QLabel* zoomLabel = new QLabel(tr("Zoom:"), this);

          m_zoomSpin = new QSpinBox(this);
          m_zoomSpin->setKeyboardTracking(false);
          m_zoomSpin->setAccelerated(true);
          m_zoomSpin->setSuffix(tr("%"));
          m_zoomSpin->setMinimum(0);
          m_zoomSpin->setMaximum(DefaultMaxZoomLevel);
          m_zoomSpin->setValue(m_zoomLevel);
          m_zoomSpin->setSpecialValueText(tr("Zoom to fit"));
          connect(m_zoomSpin, SIGNAL(valueChanged(int)), this, SLOT(setZoomLevel(int)));
          connect(this, SIGNAL(zoomLevelChanged(int)), m_zoomSpin, SLOT(setValue(int)));

          m_zoomSlider = new QSlider(Qt::Horizontal, this);
          m_zoomSlider->setMinimum(0);
          m_zoomSlider->setMaximum(DefaultMaxZoomLevel);
          m_zoomSlider->setValue(m_zoomLevel);
          connect(m_zoomSlider, SIGNAL(valueChanged(int)), m_zoomSpin, SLOT(setValue(int)));
          connect(m_zoomSpin, SIGNAL(valueChanged(int)), m_zoomSlider, SLOT(setValue(int)));

          QGridLayout* layout = new QGridLayout(this);
          layout->addWidget(zoomLabel, 0, 0, 1, 1);
          layout->addWidget(m_zoomSpin, 0, 1, 1, 1);
          layout->addWidget(m_zoomSlider, 1, 0, 1, 2);
          return;
     }

}
